/**
 * Datenbank-Schnittstelle
 */
package com.sas.hnn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PicsDbAdapter {

	public static final String KEY_WAHL = "wahl";
	public static final String KEY_PARTY = "party";
	public static final String KEY_ROWID = "_id";

	private static final String TAG = PicsDbAdapter.class.getSimpleName();
	private DatabaseHelper mDbHelper;

	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "data";
	private static final String DATABASE_TABLE = "party";
	private static final int DATABASE_VERSION = 2;

	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (_id integer primary key autoincrement, "
			+ KEY_WAHL + " varchar(32), " + KEY_PARTY + " varchar(32));";

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.d(TAG, "in onCreate()");
			db.execSQL(DATABASE_CREATE);

			// schreibe Initialdaten
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_WAHL, "Bundestagswahl");
			initialValues.put(KEY_PARTY, "no_choice");

			db.insert(DATABASE_TABLE, null, initialValues);

			initialValues = new ContentValues();
			initialValues.put(KEY_WAHL, "Landtagswahl");
			initialValues.put(KEY_PARTY, "no_choice");

			db.insert(DATABASE_TABLE, null, initialValues);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.d(TAG, "in onUpgrade()");
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

		@Override
		public synchronized void close() {
			super.close();
		}

	}

	public PicsDbAdapter(Context ctx) {
		Log.d(TAG, "im Konstruktor()");
		this.mCtx = ctx;
	}

	public PicsDbAdapter open() throws SQLException {
		Log.d(TAG, "in open()");
		mDbHelper = new DatabaseHelper(mCtx);
		if (mDb == null)
			mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		Log.d(TAG, "in close()");
		mDbHelper.close();
	}

	public long createWahl(String wahl, String party) {
		Log.d(TAG, "in createWahl()");
		long rowId = -1;
		try {
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_WAHL, wahl);
			initialValues.put(KEY_PARTY, party);

			rowId = mDb.insert(DATABASE_TABLE, null, initialValues);

		} catch (SQLiteException e) {
			Log.d(TAG, "createNote() - insert(): ", e);
		} finally {
			Log.d(TAG, "createNote() - insert(): rowId=" + rowId);
		}
		return rowId;
	}

	public boolean deleteWahl(long rowId) {
		Log.d(TAG, "in deleteWahl()");
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAllWahlen() {
		Log.d(TAG, "in fetchAllWahlen()");
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_WAHL,
				KEY_PARTY }, null, null, null, null, null);
	}

	public Cursor fetchWahl(long rowId) throws SQLException {
		Log.d(TAG, "in fetchWahl()");
		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_WAHL,
				KEY_PARTY }, KEY_ROWID + "=" + rowId, null, null, null, null,
				null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public boolean updateWahl(String wahl, String party) {
		Log.d(TAG, "in updateWahl()");
		ContentValues args = new ContentValues();
		args.put(KEY_PARTY, party);
		return mDb.update(DATABASE_TABLE, args, KEY_WAHL + "='" + wahl + "'",
				null) > 0;
	}
}
